﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using League.Entity;
using System.Data.Linq;
using League.DBUtility;
using System.Data;
using System.Data.SqlClient;
using JJY.DBUtility;

namespace League.DataAccess
{
    public class LeagueTeamDataAccess : SQLEntityHelper<LeagueTeam>
    {
        public static Dictionary<string, string> BatchInsert(string leagueID, params string[] teamNameSet)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();

            //SqlParameter[] parms = {
            //                           new SqlParameter("LeagueID", leagueID),
            //                           new SqlParameter("TeamNameList", string.Join(",", teamNameSet))
            //                       };

            using (SqlDataReader dr = LeagueSQLHelper.ExecuteReader(CommandType.Text,
                string.Format("EXEC LeagueTeamNameBatchInsert N'{0}',N'{1}'", leagueID, string.Join(",", teamNameSet))))
            //using (SqlDataReader dr = LeagueSQLHelper.ExecuteReader(CommandType.StoredProcedure, "LeagueTeamNameBatchInsert", parms))
            {
                while (dr.Read())
                {           
                    if(!result.ContainsKey(dr[1].ToString()))
                    {
                        result.Add(dr[1].ToString(), dr[0].ToString());
                    }
                }
            }
            return result;
        }

        public static List<LeagueTeam> GetAllTeamByLeagueID(Guid leagueID)
        {
            string sql = "SELECT * FROM LeagueTeam t " +
                        "LEFT JOIN LeagueMatch m ON t.TeamID = m.HomeTeamID OR t.TeamID = m.VisitingTeamID " +
                        "WHERE m.RoundNumber =1 AND m.LeagueID = '" + leagueID.ToString() + "'";
            return LeagueSQLEntityHelper<LeagueTeam>.GetEntitySet(sql);
        }
    }
}
